import platform
import csv


class AverageMeter(object):
    """
    Keeps track of most recent, average, sum, and count of a metric.
    """

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count


class Logger(object):
    def __init__(self, filepath, headers):
        self.log_file = open(filepath, 'w')
        self.logger = csv.writer(self.log_file, delimiter='\t')
        self.logger.writerow(headers)
        self.headers = headers
    
    def __del(self):
        self.log_file.close()

    def log(self, values):
        write_values = []
        for name in self.headers:
            assert name in values
            write_values.append(values[name])
        self.logger.writerow(write_values)
        self.log_file.flush()


def get_system_info():
    info = {}
    info['system_name'] = platform.system()

    return info
